var dbConfig = require('../utils/dbconfig');

// 获取用户列表接口
getUserList = (req,res)=>{
    console.log('获取用户列表接口 - - -  接到请求');
    //获取请求参数
    let {queryParam,pageNum,pageSize} = req.query
    var sql_total = "select count(1) total from user_info ";
    var sql = "select t1.user_id,t1.user_name,t1.phone,t1.email,t1.role,case when t1.user_status='1' then 'true' else 'false' end user_status,t2.role_name from user_info t1 left join role_info t2 on t1.role_id = t2.role_id";
    var common_sql = "";
    var sqlArr = [];
    if(queryParam !== ''){
        common_sql += " where user_name like '%"+queryParam+"%'"
        // sqlArr.push(queryParam)
    }
    var limit_sql = " limit ?,?"
    sqlArr.push(pageNum*1-1);
    sqlArr.push(pageSize*1);
    var listdata = [];
    var callBack2 = (err,data) =>{
        if(err){
            console.log('链接出错了~')
        }else{
            res.send({
                'code':200,
                'total':data[0].total,
                'list':listdata
            })
        }
    }
    var callBack1 = (err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            listdata = data;
            dbConfig.sqlConnect(sql_total+common_sql,sqlArr,callBack2);
        }
    }


    console.log("查询用户列表SQL:"+sql+common_sql+limit_sql)
    console.log("查询用户列表SQLArr:"+sqlArr)
    dbConfig.sqlConnect(sql+common_sql+limit_sql,sqlArr,callBack1);
}


updateUserStatus = (req,res) =>{
    console.log("修改用户状态接口 --- 接到请求")
    let {user_status,user_id} = req.body
    var sql = "update user_info set user_status=? where user_id = ?";
    var sqlArr = [user_status,user_id];

    console.log("修改用户状态接口SQL:"+sql)
    console.log("修改用户状态接口SQLArr:"+sqlArr)
    dbConfig.sqlConnect(sql,sqlArr,(err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            res.send({
                'code':200,
                'data':data[0]
            })
        }
    });
}


addUser = (req,res) =>{
    console.log("添加用户接口 --- 接到请求")
    let {username,password,email,phone} = req.body
    var sql = "insert into user_info(`user_id`,`user_name`,`password`,`email`,`phone`,`user_status`) values (?,?,?,?,?,'1')";
    var sqlArr = [username,username,password,email,phone];

    console.log("添加用户接口SQL:"+sql)
    console.log("添加用户接口SQLArr:"+sqlArr)
    dbConfig.sqlConnect(sql,sqlArr,(err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            res.send({
                'code':200,
                'data':data[0]
            })
        }
    });
}

queryUserById = (req,res) =>{
    console.log("根据ID查询用户信息接口 --- 接到请求")
    let {user_id} = req.query
    var sql = "select * from user_info where user_id =?";
    var sqlArr = [user_id];

    console.log("根据ID查询用户信息接口SQL:"+sql)
    console.log("根据ID查询用户信息接口SQLArr:"+sqlArr)
    dbConfig.sqlConnect(sql,sqlArr,(err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            res.send({
                'code':200,
                'data':data[0]
            })
        }
    });
}

updateUserMsg = (req,res) =>{
    console.log("修改用户信息接口 --- 接到请求")
    let {email,phone,user_id} = req.body
    var sql = "update user_info set email=? ,phone=? where user_id = ?";
    var sqlArr = [email,phone,user_id];

    console.log("修改用户信息接口SQL:"+sql)
    console.log("修改用户信息接口SQLArr:"+sqlArr)
    dbConfig.sqlConnect(sql,sqlArr,(err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            res.send({
                'code':200,
                'data':data[0]
            })
        }
    });
}

deleteUser = (req,res) =>{
    console.log("删除用户信息接口 --- 接到请求")
    let {user_id} = req.query
    var sql = "delete from user_info where user_id=?";
    var sqlArr = [user_id];

    console.log("删除用户信息接口SQL:"+sql)
    console.log("删除用户信息接口SQLArr:"+sqlArr)
    dbConfig.sqlConnect(sql,sqlArr,(err,data) => {
        if(err){
            console.log('链接出错了~')
        }else{
            res.send({
                'code':200,
                'data':data[0]
            })
        }
    });
}
module.exports ={
    getUserList,updateUserStatus,addUser,queryUserById,updateUserMsg,deleteUser
}
